package cn.iocoder.yudao.module.engine.service.core.aichat.client.impl;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.engine.model.context.AIMessageContext;
import cn.iocoder.yudao.module.engine.model.aichat.AIChatReplayMessage;
import cn.iocoder.yudao.module.engine.service.core.aichat.client.ILLMChatClient;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.io.IOException;

public class EmitterILLMChatClient implements ILLMChatClient {
    private final ResponseBodyEmitter emitter;

    public EmitterILLMChatClient(ResponseBodyEmitter emitter) {
        this.emitter = emitter;
    }

    @Override
    public void complete(AIMessageContext messageCtx) {
        emitter.complete();
    }

    @Override
    public void completeWithError(AIMessageContext messageCtx, Throwable ex) {
        emitter.completeWithError(ex);
    }

    @Override
    public void send(AIMessageContext messageCtx, AIChatReplayMessage res) throws IOException {
        emitter.send(JsonUtils.toJsonString(res));
    }
}
